﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin.User
{
    public partial class User_Index : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]);
                int UserID = Input.SetInt(Request["ID"]);
                Bind_Group();
                switch (Action.ToLower().Trim())
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        if (UserID > 0)
                        {
                            Bind_DataEdit(UserID);
                        }
                        break;
                    default:
                        this.Bind_gvList();
                        break;
                }
            }
        }
        private void Bind_Group()
        {
            this.ddlGroupID.DataSource = new BLLUserGroup().GetList("");
            this.ddlGroupID.DataTextField = "GroupName";
            this.ddlGroupID.DataValueField = "ID";
            this.ddlGroupID.DataBind();

            this.ddlGroupID.Items.Insert(0, new ListItem("普通会员", "0"));
        }
        //绑定
        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int count = 0;
            this.gvList.DataSource = new BLLUser().GetList(Pager.PageSize,Pager.CurrentPageIndex,"",ref count);
            Pager.RecordCount = count;
            this.gvList.DataBind();
        }
        //分院绑定
        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_gvList();
        }
        //数据编辑
        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelUser model = new BLLUser().GetModel(id);
            if (model != null)
            {
                this.txtID.Value = model.ID.ToString();
                this.txtUserName.Text = model.UserName;
                //this.txtUserPassword.Text = model.UserPassword;
                this.txtUserScore.Text = model.UserScore.ToString();
                this.txtUserEmail.Text = model.UserEmail;
                this.ddlGroupID.SelectedValue = model.GroupID.ToString();
                this.txtUserQuestion.Text = model.Question;
                this.txtUserAnswer.Text = model.Answer;
                this.txtUserICQ.Text = model.QQ;

                this.btnUserSave.Text = "修改保存";
            }
        }
        //数据删除
        private void Bind_Delete(int id)
        {
            new BLLUser().Delete(id);
            this.Bind_gvList();
        }
        //保存 编辑
        protected void btnUserSave_Click(object sender, EventArgs e)
        {
            int ID = int.Parse(this.txtID.Value);
            string UserName = this.txtUserName.Text;
            string UserPassword = this.txtUserPassword.Text;
            string UserPassword2 = this.txtUserPassword2.Text;
            DateTime RegDate = DateTime.Now;
            int UserScore = int.Parse(this.txtUserScore.Text);
            int ScoreUsed = 0;
            string UserEmail = this.txtUserEmail.Text;
            int UserSex = 1;
            DateTime LoginDate = DateTime.Now;
            int LoginTimes = 0;
            string IdentityCard = "";
            int PostCode = 100000;
            int AreaID = 0;
            string Address = "";
            int IsLock = 0;
            int GroupID = Input.SetInt(this.ddlGroupID.SelectedValue);
            string Question = this.txtUserQuestion.Text;
            string Answer = this.txtUserAnswer.Text;
            string LoginIP = "";
            decimal FrozenDeposit = 0;
            int FrozenScore = int.Parse("0");
            string BirthDay = "";
            string QQ = this.txtUserICQ.Text;
            string Telephone = "";
            string Phone = "";
            string TrueName = "";
            string CommendUser = "";
            string NewPassWord = "";
            string GUID = "";
            if (UserPassword.Trim().Length > 0)
            {
                if (UserPassword2.Trim() == "")
                {
                    Pr.RegClientAlert(this, "确认密码不能为空!");
                    return;
                }

                if (UserPassword.Trim() != UserPassword2.Trim())
                {
                    Pr.RegClientAlert(this, "两次输入的密码不相同!");
                    return;
                }
            }
            
            BLLUser bll = new BLLUser();
            ModelUser model = new ModelUser();
            model.UserName = UserName;
            model.UserPassword = UserPassword;
            model.RegDate = RegDate;
            model.UserScore = UserScore;
            model.ScoreUsed = ScoreUsed;
            model.UserEmail = UserEmail;
            model.UserSex = UserSex;
            model.LoginDate = LoginDate;
            model.LoginTimes = LoginTimes;
            model.IdentityCard = IdentityCard;
            model.PostCode = PostCode;
            model.AreaID = AreaID;
            model.Address = Address;
            model.IsLock = IsLock;
            model.GroupID = GroupID;
            model.Question = Question;
            model.Answer = Answer;
            model.LoginIP = LoginIP;
            model.FrozenDeposit = FrozenDeposit;
            model.FrozenScore = FrozenScore;
            model.BirthDay = BirthDay;
            model.QQ = QQ;
            model.Telephone = Telephone;
            model.Phone = Phone;
            model.TrueName = TrueName;
            model.CommendUser = CommendUser;
            model.NewPassWord = NewPassWord;
            model.GUID = GUID;
            if (ID == 0)
            {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (ID > 0)
            {
                ModelUser smodel = bll.GetModel(ID);
                model.UserPassword = smodel.UserPassword;
                model.ID = ID;
                bll.Update(model);
            }
            this.Bind_gvList();
        }
        //执行行命令
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }
    }
}
